define(window.backboneCommon.getViewDefine("account.login"), function ($,require,Collection,tpl) {
    var backboneView= Backbone.View.extend({
        events: {
            'keydown #txt_username': 'enterKeyword',
            'keydown #txt_password': 'enterKeyword',
            'click #btn_login': 'submitLogin'
        },
        initialize: function () {
            this.listenTo(this.model, 'login:Info', this.loginCallback);
            this.render();
        },
        render:function(json){
            var templateHtml=_.template(tpl);
            this.$el.html(templateHtml(json));
            return this;
        },
        initMain:function(){
            $.cookie('usertoken',false);
            this.extObject={};
            this.elementModel={};
            this.elementModel.formLogin=$("#form_login");
            this.extObject.formLogin=this.elementModel.formLogin.ExtParsley();
            this._initPanelHeight();
            this._showPanelPage();
            $.ExtAssist.urlParam.cleanParam();
        },
        _initPanelHeight:function(){
            setTimeout(function(){
                var element=$("#panel_loginBody");
                element.parent().css("margin-top",($("body").height()-60-element.height())/2);
                element.show()
            },100);
        },
        _showPanelPage:function () {
            this.elementModel.formLogin.find(":text").val("");
            $("#panel_login").show().addClass("fadeInLeftSlight");
        },
        enterKeyword:function(e){
            if(e.keyCode === 13) {
                this.submitLogin()
            }
        },
        _checkLoginFrom:function(){
            $.ExtNotify.destroy();
            return this.extObject.formLogin.validate();
        },
        submitLogin:function(){
            var self=this;
            if(this._checkLoginFrom()){
                var ajaxdata =this.elementModel.formLogin.ExtFormValue().getValues();
                this.extObject.login=$("#btn_login").ExtLadda();
                this.extObject.login.start();
                setTimeout(function(){self.collection.submitLogin(ajaxdata)},200);
            }
        },
        loginCallback: function () {
            this.extObject.login.stop();
            var res=this.model.get("dataLogin");
            if (res.success) {
                $.ExtAssist.goUrlHref("#",true,true);
            }else{
                $.ExtNotify.error(res.msg);
            }
        }
    });
    var viewModel=function(){
        this.collection= new Collection();
        this.model=this.collection.model;
        this.view=new backboneView({el: '#login_container_body',collection:this.collection,model:this.model});
    };
    viewModel.prototype={
        initView:function () {
            this.view.initMain();
        }
    };
    return viewModel;
});